package org.osgroup.controller;

import org.osgroup.project.Restaurant;
import org.osgroup.service.FoodStreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 餐厅信息接口控制器
 */
@RestController
@RequestMapping("/api/restaurant")
@CrossOrigin(origins = "*")
public class RestaurantController {
    
    @Autowired
    private FoodStreetService foodStreetService;
    
    /**
     * 获取所有餐厅列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<Map<String, Object>>> getRestaurantList() {
        try {
            List<Restaurant> restaurants = foodStreetService.getFoodStreet().getAllRestaurants();
            List<Map<String, Object>> result = new ArrayList<>();
            
            for (Restaurant r : restaurants) {
                Map<String, Object> info = new HashMap<>();
                info.put("id", r.getRestaurantId());
                info.put("name", r.getRestaurantName());
                info.put("isOpen", r.isOpen());
                
                // 菜单信息
                List<Map<String, Object>> menuList = new ArrayList<>();
                for (Restaurant.MenuItem item : r.getMenuItems()) {
                    Map<String, Object> menuItem = new HashMap<>();
                    menuItem.put("itemId", item.getItemId());
                    menuItem.put("itemName", item.getItemName());
                    menuItem.put("price", item.getPrice());
                    menuItem.put("preparationTime", item.getPreparationTime());
                    menuItem.put("description", item.getDescription());
                    menuList.add(menuItem);
                }
                info.put("menu", menuList);
                
                result.add(info);
            }
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
    }
    
    /**
     * 根据名称获取餐厅详情
     */
    @GetMapping("/detail")
    public ResponseEntity<Map<String, Object>> getRestaurantByName(@RequestParam String name) {
        try {
            List<Restaurant> restaurants = foodStreetService.getFoodStreet().getAllRestaurants();
            for (Restaurant r : restaurants) {
                if (r.getRestaurantName().equals(name)) {
                    Map<String, Object> info = new HashMap<>();
                    info.put("id", r.getRestaurantId());
                    info.put("name", r.getRestaurantName());
                    info.put("isOpen", r.isOpen());
                    
                    // 菜单信息
                    List<Map<String, Object>> menuList = new ArrayList<>();
                    for (Restaurant.MenuItem item : r.getMenuItems()) {
                        Map<String, Object> menuItem = new HashMap<>();
                        menuItem.put("itemId", item.getItemId());
                        menuItem.put("itemName", item.getItemName());
                        menuItem.put("price", item.getPrice());
                        menuItem.put("preparationTime", item.getPreparationTime());
                        menuItem.put("description", item.getDescription());
                        menuList.add(menuItem);
                    }
                    info.put("menu", menuList);
                    
                    return ResponseEntity.ok(info);
                }
            }
            return ResponseEntity.notFound().build();
        } catch (Exception e) {
            return ResponseEntity.status(500).build();
        }
    }
}
